package com.itheima.reggie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends
        ServiceImpl<SetmealMapper, Setmeal> implements
        SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public void addSetmeal(SetmealDto setmealDto) {
        //保存套餐的基本信息
        this.save(setmealDto);

        //套餐id
        Long setmealid = setmealDto.getId();
        //套餐菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealid);
            return item;
        }).collect(Collectors.toList());//重新转为List集合

        //保存菜品数据到setmeal_dish中
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public SetmealDto getByIdWithDish(Long id) {
        Setmeal setmeal = this.getById(id);

        SetmealDto setmealDto = new SetmealDto();
        BeanUtil.copyProperties(setmeal,setmealDto);
        LambdaQueryWrapper<SetmealDish> qwsetdish = new LambdaQueryWrapper<>();
        qwsetdish.eq(SetmealDish::getSetmealId,id);
        //select * from setmealDish where setmealId = ?
        List<SetmealDish> list = setmealDishService.list(qwsetdish);
        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    @Override
    public void updateWithdish(SetmealDto setmealDto) {
        //更新套餐表
        this.updateById(setmealDto);
        //清理当前套餐下菜品
        LambdaQueryWrapper<SetmealDish> qwdish = new LambdaQueryWrapper<>();
        qwdish.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(qwdish);
        //套餐id
        Long setmealid = setmealDto.getId();
        //套餐菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealid);
            return item;
        }).collect(Collectors.toList());//重新转为List集合

        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void delete(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> qwsetmeal = new LambdaQueryWrapper<>();
        qwsetmeal.in(Setmeal::getId,ids);
        qwsetmeal.eq(Setmeal::getStatus,1);
        int count = this.count(qwsetmeal);
        if(count>0){
            throw new CustomException("套餐为起售状态不能删除");
        }
        this.removeByIds(ids);

        LambdaQueryWrapper<SetmealDish> qwdishSetmeal = new LambdaQueryWrapper<>();
        qwdishSetmeal.in(SetmealDish::getId,ids);

        setmealDishService.remove(qwdishSetmeal);
    }


}
